<?php

namespace app\model;

use app\utils\CamlHump;
use think\facade\Db;
use think\Model;

class SysClass extends BaseModel
{
    protected $convertNameToCamel = true;
    protected $name = 'sys_class';
    static $tableTable = 'sys_class';

     // 设置字段信息
      static   $tableKeys = [
        'id'          => 'int',
        'name'        => 'string',
        'parent_id'      => 'int',
        'parent_id_path'       => 'string',
        'deep' => 'int',
        'type_info' => 'string',
        'img' => 'string',
        'order_num' => 'int',
        'remarks' => 'string',
        'info_level' => 'int'
    ];



    static function  build($data)
    {
        $emptyData = [];
        foreach (self::$tableKeys as $key => $value){
            $emptyData[CamlHump::toCamelCase($key)] = $data[CamlHump::toCamelCase($key)] ?? '';
        }


        return $emptyData;
    }
    static function  buildForUpdate($data)
    {
        $updateData = [];

        foreach (self::$tableTable as $key => $value){
            if (isset($data[CamlHump::toCamelCase($key)])) {
                $updateData[CamlHump::toCamelCase($key)] = $data[CamlHump::toCamelCase($key)];
            }
        }

        return $updateData;
    }
    static function createQuery($queryData)
    {
        $query = Db::table(self::$tableTable);
        $query->where('del_state', 1);


        foreach (self::$tableKeys as $key => $value){
            if (isset($queryData[CamlHump::toCamelCase($key)])
            ) {
                if($value=='int'){
                    if($queryData[CamlHump::toCamelCase($key)]>0){
                        $query->where($key, $queryData[CamlHump::toCamelCase($key)]);
                    }
                }else if($value=='string'){
                    if($queryData[CamlHump::toCamelCase($key)]!=''){
                        $query->where($key, $queryData[CamlHump::toCamelCase($key)]);
                    }  
                }else{
                    $query->where($key, $queryData[CamlHump::toCamelCase($key)]);
                }
            }
         
        }

        if(isset($queryData[CamlHump::toCamelCase('parentId')])){
            $query->where('parent_id', $queryData[CamlHump::toCamelCase('parentId')]);
        }




        return $query;
    }


    static function createQueryById(int $id)
    {
        $query = Db::table(self::$tableTable);
        if (isset($id)) {
            $query->where('id', $id);
        }

        return $query;
    }
    static function createQueryByParentId(int $parentId)
    {
        $query = Db::table(self::$tableTable);
        if (isset($parentId)) {
            $query->where('parent_id', $parentId);
        }

        return $query;
    }

    static function addPlus($data){
        $sysAccountAdd = self::build($data);
        $result = self::create($sysAccountAdd);
        return $result;
    }
    static function updatePlus($data, $id)
    {
        $result = self::update($data, ['id' => $id]);
        return $result;
    }



}
